var entryRepo = (function() {
	var a = typeof session != 'undefined' ? session.entries : [];
	return {
		byId: function(id) {
			var i, j = a.length, k;
			for (i = 0; i < j; i++) {
				if (a[i].id == id) {
					k = a[i];
					break;
				}
			}
			console.log('entry byId', id, k);
			return k;
		},
		firstCreatedAfter: function(created) {
			var i, j = a.length, k;
			for (i = 0; i < j; i++) {
				if (a[i].created > created && (!k || a[i].created < k.created)) k = a[i];
			}
			console.log('entry firstCreatedAfter', created, k);
			return k;
		}
	};
})();

var shotRepo = (function() {
	var a = typeof session != 'undefined' ? session.shots : [];
	return {
		at: function(position) {
		  var i, j = a.length, k = [];
		  for (i = 0; i < j; i++) {
		    if (a[i].position == position) k.push(a[i]);
		  }
		  console.log('shot at', position, k);
		  return k;
		},
		firstAfter: function(position) {
		  var i, j = a.length, k;
		  for (i = 0; i < j; i++) {
		    if (a[i].position > position && (!k || a[i].position < k.position)) k = a[i];
		  }
		  console.log('shot firstAfter', position, k);
		  return k;
		}
	};
})();

var entry, shot, index = 0, currentShots;

function show() {
	if (index >= session.entries.length) {
		return false;
	}
	//console.log('position = ' + session.position);
	currentShots = shotRepo.at(session.position);
	//console.log('currentShots = ', currentShots);
	if (currentShots.length) {
		shot = currentShots[0];
		entry = entryRepo.byId(shot.entryId);
	} else {
		entry = entryRepo.firstCreatedAfter(session.lastShotEntry ? session.lastShotEntry.created : 0);
		if (entry) {
			shot = null;
		} else {
			shot = shotRepo.firstAfter(session.position);
			if (shot) {
				entry = entryRepo.byId(shot.entryId);
			} else {
				alert('TODO 1');
			}
		}
	}
	$('#text1 span').text(entry.text1);
	$('#text2').removeClass('solution');
	$('#text2 span').eq(0).text(entry.text2);
	$('#text2 span').eq(1).text(entry.text2.replace(/[A-Za-z\u00C0-\u017F]/g, '.'));
	return true;
}

function feedback(b) {
	var v = $(b).text().toLowerCase();
	var f = $(b).closest('form');
	//var h = document.createElement('input');
	//h.type = 'hidden';
	//h.name = 'feedback' + index;
	//h.value = session.position + '_' + entry.id + '_' + v;
	//f.appendChild(h);
	f.append('<input type="hidden" name="feedback' + index + '" value="' + session.position + '_' + entry.id + '_' + v + '" />');
	var trend = shot ? shot.trend : 3;
	console.log('trend = ' + trend + ' (A)');
	trend = v == 'ok' ? trend + 1 : Math.min(trend - 1, 2);
	trend = Math.min(Math.max(1, trend), 31);
	console.log('trend = ' + trend + ' (B)');
	var delta = Math.min(1 << trend, session.entryCount);
	console.log('shotPosition = ' + (shot ? shot.position : -1) + ' (A)');
	var shotPosition = session.position + delta;
	console.log('shotPosition = ' + shotPosition + ' (B)');
	console.log('position = ' + session.position + ' (A)');
	if (currentShots.length <= 1) {
		session.position = (shot ? shot.position : session.position) + 1;
	}
	console.log('position = ' + session.position + ' (B)');
	if (!shot) {
		session.lastShotCreated = session.lastShotCreated + 1;
		session.shots.push({ entryId: entry.id, position: shotPosition, trend: trend, created: session.lastShotCreated });
		session.lastShotEntry = entry;
	} else {
		shot.position = shotPosition;
		shot.trend = trend;
	}
	index++;
	if (!show()) f.submit();
}

$(function() {
	$('#text2').click(function() {
		$(this).addClass('solution');
	});
	$('#feedback a').click(function() {
		feedback(this);
	});
	if (typeof session != 'undefined') show();
});
